<?php  

	/**
	* 城市模型
	*/
	class cityModel extends Model
	{
		public $table = 'city';

		// 保存所有城市信息 array('cityid'=>'cityname')
		public $city = null;

		// 保存顶级城市信息 array('cityid'=>'cityname')
		public $topCity = null;
		// 保存二级城市 array('pid'=>array('cityid'=>'cityname'))
		public $sonCity = null;

		public function __init()
		{
			$this->city = S('city',false,null, array('dir'=>CACHE_PATH));
			$this->topCity = S('topCity',false,null, array('dir'=>CACHE_PATH));
			$this->sonCity = S('sonCity',false,null, array('dir'=>CACHE_PATH));
		}
		public function update_cache()
		{
			// 获取所有城市信息
			$city = $this->all();
			$temp = array();
			foreach ($city as $value) {
				$temp['city'][$value['cityid']] = $value['cityname'];
				if ($value['pid'] == 0) {
					$temp['topCity'][$value['cityid']] =  $value['cityname'];
				}
				if($value['pid']>0){
					$temp['sonCity'][$value['pid']][$value['cityid']] = $value['cityname'];
				}
			}
			// p($city);
			S('city',$temp['city'],0, array('dir'=>CACHE_PATH));
			S('topCity',$temp['topCity'],0, array('dir'=>CACHE_PATH));
			S('sonCity',$temp['sonCity'],0, array('dir'=>CACHE_PATH));
			// echo CACHE_PATH;

		}

		/**
		 * 传入一级城市cityid返回其下的二级城市信息array(array('cityid'='cityid','cityname'=>'cityname'))
		 * @param  [type] $cityid [description]
		 * @return [type]         [description]
		 */
		public function get_city_data($cityid)
		{
			$sonCity = $this->sonCity;
			if($cityid != 0 ){
				$cityArr = array();
				foreach ($sonCity[$cityid] as $key => $value) {
					$cityArr[] = array(
							'cityid' => $key,
							'cityname' => $value
						);
				}
			}else{
				$cityArr[] = array('cityid' => 0,'cityname' => '请选择');
			}

			return $cityArr;
		}


		public function get_data($city_cityid)
		{
			$topCity = $this->topCity;
			$sonCity = $this->sonCity;
			$temp=$this->get_city($city_cityid);
			return $temp;
		}


		public function get_city($city_cityid)
		{
			$temp =array();
			#如果id在顶级城市里，否则在二级城市里,或者id不存在
			if (array_key_exists($city_cityid, $this->topCity)) {
				$temp['topCity'] = array('cityid'=>$city_cityid,'cityname'=>$this->topCity[$city_cityid]);
				$temp['sonCity'] = null;
				$temp['sonCityAll'] = $this->get_all_son_by_pid($city_cityid);
				return $temp;
			}
			foreach ($this->sonCity as $key => $value) {
				if(array_key_exists($city_cityid, $value)){
					foreach ($value as $k => $v) {
						if($k == $city_cityid){
							$temp['topCity'] = array('cityid'=>$key,'cityname'=>$this->topCity[$key]);
							$temp['sonCity'] = array('cityid'=>$city_cityid,'cityname'=>$v);
							$temp['sonCityAll'] = $this->get_all_son_by_pid($key);
							return $temp;
						}
					}
				}
			}

			return $temp;
		}


		public function get_all_son_by_pid($cityid)
		{
			if ($cityid === 0) {
				return false;
			}
			return $this->sonCity[$cityid];
		}

		public function get_son_by_pid($cityid)
		{
			 foreach ($this->sonCity as $key => $value) {
			 	if ($value['cityid'] == $cityid) {
			 		return $value['cityid'];
			 	}
			 }
		}


		public function get_cityname($cityid)
		{
			$temp =array();
			#如果id在顶级城市里，否则在二级城市里,或者id不存在
			if (array_key_exists($cityid, $this->topCity)) {
				return $this->topCity[$cityid];
			}
			foreach ($this->sonCity as $key => $value) {
				if(array_key_exists($cityid, $value)){
					foreach ($value as $k => $v) {
						if($k == $cityid){
							$temp['topCity'] =$this->topCity[$key]; 
							$temp['sonCity'] = $v;

							break;
						}
					}
				}
			}
			$cityname = implode('/', $temp);
			// p($cityname);
			return $cityname;
		}



		public function get_city_id($pid)
		{
			// p($this->sonCity);
			$temp = array();
			foreach ($this->sonCity as $key => $value) {
				if($key == $pid){
						$temp = array_keys($value);
				}
			}
			$temp[] = $pid;
			return $temp;
		}



		public function get_citynum(){
		//读取缓存
		$citynum = S('citynum',false,0,array('dir'=>CACHE_PATH));
		if(!$citynum){

			$where = array(
				'showstate'		=>	1,//开启状态
				'verifystate'	=>	2,//审核通过
				'endtime'		=>	array('gt'=>time())
			);

			$temp = array();
			$composModel = K('ComPos');
			foreach ($this->topCity as $k => $v) {
				//得出每个顶级的子级id
				$cityidArr = $this->get_city_id($k);
				
				
				$temp[$k]['name'] = $v;
				//统计总数
				$temp[$k]['num'] = $composModel->where($where)->in(array('city_cityid'=>$cityidArr))->count();
			}

			//设置缓存
			S('citynum',$temp,30,array('dir'=>CACHE_PATH));
			$citynum = $temp;
		}
	
		return $citynum;
	}
	}
?>